package com.adminx.controller.admin;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.adminx.entity.Resource;
import com.adminx.pojo.NavigationMenuPojo;
import com.adminx.pojo.ResourcePojo;
import com.adminx.security.MyAuthResoMapping;
import com.adminx.service.MyUserDetailsService;
import com.adminx.service.ResourceService;
import com.adminx.util.CodeUtil;
import com.adminx.util.Constant;
import com.adminx.util.PageResults;


@Controller
@RequestMapping("resource")
public class ResourceController {
	
	@Autowired
	private ResourceService resourceService;
	
	@Autowired
	private MyUserDetailsService userDetailsService;
	
	/**
	 * 查询资源
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/init", method = RequestMethod.GET) 
	public String queryNavigation(ModelMap map){
		NavigationMenuPojo navigationMenuPojo = this.resourceService.queryNavigation("/resource/init.html");
		map.put("navigationMenuPojo", navigationMenuPojo);
		map.put("url", "/resource/init.html");
		return "admin/resource/resource";
	}
	
	
	/**
	 * 查询资源列表
	 * @param map
	 * @return 角色查询列表页面
	 */
	@ResponseBody
	@RequestMapping(value = "/query", method = RequestMethod.GET) 
	public Map<String, Object> query(Integer pageIndex, Integer pageSize, String searchText, Long parentId){
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			PageResults<ResourcePojo> result = this.resourceService.query(pageIndex, pageSize, CodeUtil.transcoding(searchText), parentId);
			map.put("result", result);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("operateMsg", Constant.FAIL_CODE);
		}
		return map;
	}
	
	/**
	 * 查询资源详情
	 * @return Resource
	 */
	@ResponseBody
	@RequestMapping(value = "/info", method = RequestMethod.GET) 
	public Map<String, Object> info(Long resourceId){
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Resource resource = this.resourceService.info(resourceId);
			map.put("resource", resource);
			map.put("operateMsg", Constant.SUCCESS_CODE);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("operateMsg", Constant.FAIL_CODE);
		}
		return map;
	}
	
	/**
	 * 修改资源
	 */
	@ResponseBody
	@RequestMapping(value = "/save", method = RequestMethod.POST) 
	public Map<String, Object> save(Resource resource){
		Map<String, Object> map = new HashMap<String, Object>();
		try{
			this.resourceService.save(resource);
			map.put("operateMsg", Constant.SUCCESS_CODE);
			
			//重新加载权限资源映射关系
			MyAuthResoMapping.setResourceMap(userDetailsService.loadResourcesAndAuthority());
			
		}catch(Exception e){
			e.printStackTrace();
			map.put("operateMsg", Constant.FAIL_CODE);
		}
		return map;
	}
	
	/**
	 * 新增资源
	 * @param account
	 * @return 
	 */
	@RequestMapping(value = "/add") 
	@ResponseBody
	public Map<String, Object> add(Resource resource){
		Map<String, Object> map = new HashMap<String, Object>();
		try{
			this.resourceService.add(resource);
			map.put("operateMsg", Constant.SUCCESS_CODE);
		}catch(Exception e){
			e.printStackTrace();
			map.put("operateMsg", Constant.FAIL_CODE);
		}
		return map;
	}
	
	/**
	 * 删除资源
	 */
	@RequestMapping(value = "/delete") 
	@ResponseBody
	public Map<String, Object> delete(String ids){
		Map<String, Object> map = new HashMap<String, Object>();
		try{
			int delNum = this.resourceService.delBatch(ids);
			map.put("delNum", delNum);
			map.put("operateMsg", Constant.SUCCESS_CODE);
		}catch(Exception e){
			e.printStackTrace();
			map.put("operateMsg", Constant.FAIL_CODE);
		}
		return map;
	}
}
